using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using XUCore.Ddd.Domain;
using XUCore.NetCore.DynamicWebApi;
using XUCore.NetCore.FreeSql.Curd;
using XUCore.NetCore.FreeSql.Entity;
using XUCore.Paging;

namespace XUCore.NetCore.FreeSql
{
    /// <summary>
    /// CURD API 服务<para></para>
    /// </summary>
    /// <typeparam name="TKey">主键类型</typeparam>
    /// <typeparam name="TUKey">用户主键类型</typeparam>
    /// <typeparam name="TEntity">数据库实体</typeparam>
    /// <typeparam name="TUser">IUser</typeparam>
    [DynamicWebApi]
    public abstract class AbstractCurdUserAppService<TKey, TUKey, TEntity, TUser> : IDynamicWebApi, IScoped
        where TEntity : EntityFull<TKey, TUKey>, new()
        where TUser : class, IUser
    {
        public readonly TUser User;
        /// <summary>
        /// curd
        /// </summary>
        public CurdEntity<TKey, TUKey, TEntity, TUser> curd;
        /// <summary>
        /// CURD API 服务<para></para>
        /// </summary>
        /// <param name="serviceProvider"></param>
        public AbstractCurdUserAppService(IServiceProvider serviceProvider)
            : this(serviceProvider, serviceProvider.GetRequiredService<IFreeSql>())
        {
        }
        /// <summary>
        /// CURD API 服务<para></para>
        /// </summary>
        /// <param name="serviceProvider"></param>
        /// <param name="freeSql"></param>
        public AbstractCurdUserAppService(IServiceProvider serviceProvider, IFreeSql freeSql)
        {
            var mapper = serviceProvider.GetRequiredService<IMapper>();
            this.User = serviceProvider.GetService<TUser>();
            this.curd = new CurdEntity<TKey, TUKey, TEntity, TUser>(freeSql, mapper, User);
        }
    }
}